home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Recortar, arrastrar y colocar / ClipView / ClipView.cs next >
Encoding:
Text File  |  2002-05-30  |  5.8 KB  |  171 lines

  1. //---------------------------------------
  2. // ClipView.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Windows.Forms;
  9.  
  10. class ClipView: Form
  11. {
  12.      string[] astrFormats = 
  13.      { 
  14.      DataFormats.Bitmap, DataFormats.CommaSeparatedValue, DataFormats.Dib,
  15.      DataFormats.Dif, DataFormats.EnhancedMetafile, DataFormats.FileDrop, 
  16.      DataFormats.Html, DataFormats.Locale, DataFormats.MetafilePict, 
  17.      DataFormats.OemText, DataFormats.Palette, DataFormats.PenData, 
  18.      DataFormats.Riff, DataFormats.Rtf, DataFormats.Serializable, 
  19.      DataFormats.StringFormat, DataFormats.SymbolicLink, DataFormats.Text, 
  20.      DataFormats.Tiff, DataFormats.UnicodeText, DataFormats.WaveAudio 
  21.      };
  22.      
  23.      Panel         panelDisplay;
  24.      RadioButton[] aradio;
  25.      RadioButton   radioChecked;
  26.      [STAThread]
  27.  
  28.      public static void Main()
  29.      {
  30.           Application.Run(new ClipView());
  31.      }
  32.      public ClipView()
  33.      {
  34.           Text = "Visor del portapapeles";
  35.  
  36.                // Crear un panel de ancho variable para mostrar el portapapeles.
  37.  
  38.           panelDisplay = new Panel();
  39.           panelDisplay.Parent = this;
  40.           panelDisplay.Dock = DockStyle.Fill;
  41.           panelDisplay.Paint += new PaintEventHandler(PanelOnPaint);
  42.           panelDisplay.BorderStyle = BorderStyle.Fixed3D;
  43.  
  44.                // Crear el divisor.
  45.  
  46.           Splitter split = new Splitter();
  47.           split.Parent = this;
  48.           split.Dock = DockStyle.Left;
  49.  
  50.                // Crear el panel para los botones de opci≤n.
  51.  
  52.           Panel panel = new Panel();
  53.           panel.Parent = this;
  54.           panel.Dock = DockStyle.Left;
  55.           panel.Width = 200;
  56.  
  57.                // Crear los botones de opci≤n.
  58.           
  59.           aradio = new RadioButton[astrFormats.Length];
  60.           EventHandler eh = new EventHandler(RadioButtonOnClick);
  61.  
  62.           for (int i = 0; i < astrFormats.Length; i++)
  63.           {
  64.                aradio[i] = new RadioButton();
  65.                aradio[i].Parent = panel;
  66.                aradio[i].Location = new Point(4, 12 * i);
  67.                aradio[i].Size = new Size(300, 12);
  68.                aradio[i].Click += eh;
  69.                aradio[i].Tag = astrFormats[i];
  70.           }
  71.                // Establecer tama±o base de la autoescala.
  72.  
  73.           AutoScaleBaseSize = new Size(4, 8);
  74.  
  75.                // Establecer el intervalo a 1 segundo.
  76.  
  77.           Timer timer = new Timer();
  78.           timer.Interval = 1000;
  79.           timer.Tick += new EventHandler(TimerOnTick);
  80.           timer.Enabled = true;
  81.      }
  82.      void TimerOnTick(object obj, EventArgs ea)
  83.      {
  84.           IDataObject data = Clipboard.GetDataObject();
  85.  
  86.           for (int i = 0; i < astrFormats.Length; i++)
  87.           {
  88.                aradio[i].Text = astrFormats[i];
  89.                aradio[i].Enabled = data.GetDataPresent(astrFormats[i]);
  90.  
  91.                if (aradio[i].Enabled)
  92.                {
  93.                     if (!data.GetDataPresent(astrFormats[i], false))
  94.                          aradio[i].Text += "*";
  95.  
  96.                     object objClip = data.GetData(astrFormats[i]);
  97.  
  98.                     try
  99.                     {
  100.                          aradio[i].Text += " (" + objClip.GetType() + ")";
  101.                     }
  102.                     catch
  103.                     {
  104.                          aradio[i].Text += " (íExcepci≤n en GetType!)";
  105.                     }
  106.                }
  107.           }
  108.           panelDisplay.Invalidate();
  109.      }
  110.      void RadioButtonOnClick(object obj, EventArgs ea)
  111.      {
  112.           radioChecked = (RadioButton) obj;
  113.           panelDisplay.Invalidate();
  114.      }
  115.      void PanelOnPaint(object obj, PaintEventArgs pea)
  116.      {
  117.           Panel    panel = (Panel) obj;
  118.           Graphics grfx  = pea.Graphics;
  119.           Brush    brush = new SolidBrush(panel.ForeColor);
  120.  
  121.           if (radioChecked == null || !radioChecked.Enabled)
  122.                return;
  123.  
  124.           IDataObject data = Clipboard.GetDataObject();
  125.  
  126.           object objClip = data.GetData((string) radioChecked.Tag);
  127.  
  128.           if (objClip == null)
  129.                return;
  130.  
  131.           else if (objClip.GetType() == typeof(string))
  132.           {
  133.                grfx.DrawString((string)objClip, Font, brush, 
  134.                                panel.ClientRectangle);
  135.           }
  136.           else if (objClip.GetType() == typeof(string[]))   // Arrastrar archivo
  137.           {
  138.                string str = string.Join("\r\n", (string[]) objClip);
  139.  
  140.                grfx.DrawString(str, Font, brush, panel.ClientRectangle);
  141.           }
  142.           else if (objClip.GetType() == typeof(Bitmap) ||
  143.                    objClip.GetType() == typeof(Metafile) ||
  144.                    objClip.GetType() == typeof(Image))
  145.           {
  146.                grfx.DrawImage((Image)objClip, 0, 0);
  147.           }
  148.           else if (objClip.GetType() == typeof(MemoryStream))
  149.           {
  150.                Stream stream = (Stream) objClip;
  151.                byte[] abyBuffer = new byte[16];
  152.                long   lAddress = 0;
  153.                int    iCount;
  154.                Font   font = new Font(FontFamily.GenericMonospace, 
  155.                                     Font.SizeInPoints);
  156.                float  y = 0;
  157.  
  158.                while ((iCount = stream.Read(abyBuffer, 0, 16)) > 0)
  159.                {
  160.                     string str = HexDump.ComposeLine(lAddress, abyBuffer, 
  161.                                                                iCount);
  162.                     grfx.DrawString(str, font, brush, 0, y);
  163.                     lAddress += 16;
  164.                     y += font.GetHeight(grfx);
  165.  
  166.                     if (y > panel.Bottom)
  167.                          break;
  168.                }
  169.           }
  170.      }
  171. }